<?php

/**
 * layui上传多图
 * @author yupoxiong<i@yufuping.com>
 */

namespace generate\field;

class LayuiImage extends Field
{
    public static $html = <<<EOF
  
    <div class="form-group layui-form">
    <label for="pic" class="col-sm-2 control-label">[FORM_NAME]</label>
    <div class="col-sm-10 col-md-4">
        <div class="layui-input-inline">
            <div class="layui-upload-list layui-btn" id="[FIELD_NAME]">
                多图上传
            </div>
        </div>
        <div class="">
            <ul id="imgZmList" {if isset(\$data) && isset(\$data.[FIELD_NAME]) && !empty(\$data.[FIELD_NAME]) && \$data.[FIELD_NAME] !=''} class="imgZmList" {/if}>
                {if isset(\$data) && isset(\$data.[FIELD_NAME]) && !empty(\$data.[FIELD_NAME]) && \$data.[FIELD_NAME] !=''} {foreach \$data.[FIELD_NAME] as \$vo} <li
                style="position:relative"><img name="imgZmList[]" src="{\$vo}" width="150"
                    height="120">
                <div class="img_close" onclick="deleteElement(this)">X</div><input
                    class="imghidden" type="hidden" name="[FIELD_NAME][]" value="{\$vo}"></li>
                {/foreach}
                {/if}

            </ul>
        </div>
    </div>
</div>

{js href="__ADMIN_PLUGINS__/layui/layui.js" /}
{js href="__ADMIN_JS__/move.js" /}
{js href="__ADMIN_JS__/publishImg.js" /}
{js href="__ADMIN_CSS__/publish.css" /}
<script>

$(function () {
    if(typeof(layui)=="undefined"){
    location.reload(true)
    }
    layui.use(['form', 'upload'], function () {
        var form = layui.form,
            $ = layui.jquery,
            upload = layui.upload;

        //证明多图片上传
        upload.render({
            elem: '#[FIELD_NAME]',
            url: "{:url('upfile/uploadImg')}",
            multiple: true,
            before: function (obj) {},
            done: function (res) {
                $('#imgZmList').append(
                    '<li style="position:relative"><img name="imgZmList" src="' +
                    res.data.url +
                    '"width="150" height="120"><div class="img_close" onclick="deleteElement(this)">X</div><input class="imghidden" type="hidden" name="[FIELD_NAME][]" value="' +
                    res.data.url + '"></li>').addClass('imgZmList');

                form.render();
                imgMove("imgZmList");
                //上传完毕
            }
        });
    });

    imgMove("imgZmList");
});
</script>

\n
EOF;


    //控制器添加上传多图
    public static $controllerAddCode = <<<EOF
            //处理layui[FORM_NAME]上传
                if(!isset(\$param['[FIELD_NAME]'])){
                    \$param['[FIELD_NAME]']= null;
                }
\n
EOF;

    //控制器修改上传多图
    public static $controllerEditCode = <<<EOF
                //处理layui[FORM_NAME]上传
                if(!isset(\$param['[FIELD_NAME]'])){
                    \$param['[FIELD_NAME]']=null;
                }
\n
EOF;


    //多图上传模型内修改器与获取器
    public static $modelAttrCode = <<<EOF
            //[FORM_NAME]获取器
            public function get[FIELD_NAME]Attr(\$value)
            {
                return json_decode(\$value,true);
            }

            //[FORM_NAME]修改器
            public function set[FIELD_NAME]Attr(\$value)
            {
                return json_encode(\$value);
            }

\n
EOF;


    public static $rules = [
        'required'   => '非空',
        'file_size'  => '文件大小限制',
        'file_image' => '图片类型',
        'regular'    => '自定义正则'
    ];


    public static function create($data)
    {
        $html = self::$html;
        $html = str_replace('[FORM_NAME]', $data['form_name'], $html);
        $html = str_replace('[FIELD_NAME]', $data['field_name'], $html);
        return $html;
    }
}
